package ying.sms;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;

/**
 * ClassName: StudentManagementSystem
 * Package: sms
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/5/17 19:12
 * @Version 1.0
 */

/*
利用Collection重构学生信息管理系统,重构以下功能

        - 添加学生
        - 添加的学生必须具备邮箱并且符合邮箱格式
        - 显示系统中所有的学生
        - 判断某个学生在系统中是否存在(根据学生姓名判断)
        - 根据学生学号查找某个学生
        - 求学生的平均成绩
        - 根据指定的入学日期获取该入学日期之前入学的学生
        - 根据指定的入学日期获取该入学日期之后入学的学生
*/


public class StudentManagementSystem02 {


    private Collection students = new ArrayList();


    public boolean validateStudentEmai(Student student)
    {
        if (student == null || student.getEmal() == null) return false;
        else
        {
            return student.getEmal().matches("\\w+@\\w+\\.+\\w+");
        }
    }


    /**
     * 向指定容器中加学生
     * @param
     * @param student
     */
    public void addStudent(Student student)
    {
        if (student != null && validateStudentEmai(student))
        {
            students.add(student);
        }
        else {
            System.out.println("学号为：" + student.getId() + " ,姓名为：" + student.getName() + " ,邮箱为: " +
                    student.getEmal() + " 的学生邮箱格式不符合，添加失败");
        }
    }



    public void showAllStudent(Collection students)
    {
        int i = 1;
        for (Object student : students)
        {
                System.out.println((i ++) + " " + student);
        }
    }

    public void showAllStudent()
    {
        showAllStudent(students);
    }


    public boolean is_contains(Student stu)
    {
//        if (stu == null) return false;
//
//        for (Object student : students)
//            if (student.equals(stu))
//                return true;
//
//        return false;
        return students.contains(stu);
    }


    public Student findStudentById(int id)
    {
        for (Object student : students)
        {
            if (student instanceof Student student1)
                if (student1.getId() == id)
                    return student1;
        }
        return null;
    }

    public int getAverageScore(String subject)
    {
        int sum = 0;
        for (Object student : students)
        {
            if (student instanceof Student stu) {
                int score = getScoreBySubjects(stu, subject);
                sum += score;
            }
        }
        return students.size() == 0 ? 0 : sum / students.size();
    }

    private int getScoreBySubjects(Student student, String subjects) { // ----------------------难点！！！
        switch(subjects)
        {
            case "语文" ->{
                return student.getChineseScore();
            }
            case "数学" ->{
                return student.getMathScore();
            }
            case "历史" ->{
                if (student instanceof MiddleStudents middleStudents)
                    return middleStudents.getHistoryScore();
            }
            case "物理" ->{
                if (student instanceof MiddleStudents middleStudents)
                    return middleStudents.getPhysicsScore();
            }
        }
        return 0;
    }

    public Collection getStudentBeforeDate(String date)
    {
        Collection tmps = new ArrayList();
        for (Object student : students) {
//            if (student != null && student.getEnrollmentDates() != null &&
//                    student.getEnrollmentDates().isBefore(LocalDate.parse(date)) ||
//                    student.getEnrollmentDates().isEqual(LocalDate.parse(date))) //LocalDate.parse(date)
//                                                                                    //不指定模式就按照默认年-月-日
//                addStudent(tmps,student);
            if (student instanceof Student stu && stu.getEnrollmentDates() != null) {
                if (stu.getEnrollmentDates().isBefore(LocalDate.parse(date)) ||
                        stu.getEnrollmentDates().isEqual(LocalDate.parse(date)))
//                    addStudent(tmps, stu);
                    tmps.add(stu);
            }

        }
        return tmps;
    }

    public Collection getStudentAfterDate(String date)
    {
        Collection tmps = new ArrayList();
        for (Object student : students) {
//            if (student != null && student.getEnrollmentDates() != null &&
//                    student.getEnrollmentDates().isBefore(LocalDate.parse(date)) ||
//                    student.getEnrollmentDates().isEqual(LocalDate.parse(date))) //LocalDate.parse(date)
//                                                                                    //不指定模式就按照默认年-月-日
//                addStudent(tmps,student);
            if (student instanceof Student stu && stu.getEnrollmentDates() != null) {
                if (stu.getEnrollmentDates().isAfter(LocalDate.parse(date)) ||
                        stu.getEnrollmentDates().isEqual(LocalDate.parse(date)))
//                    addStudent(tmps, stu);
                    tmps.add(stu);
            }

        }
        return tmps;
    }

}
